Higher-order Conditional Term Rewriting in the L Logic Programming Language Preliminary Results
نویسنده
چکیده
ion, application, a x point operator, and the let operator as in ML. We introduce a primitive type tm for terms of this functional language and introduce the constants shown with their types in Figure 3 to represent the constructs of the language. We will write and < as in x operators. Clearly not all terms of true : tm nil : tm false : tm cons : tm! tm! tm if : tm! tm! tm! tm hd : tm! tm 0 : tm tl : tm! tm s : tm! tm empty : tm! tm < : tm! tm! tm app : tm! tm! tm : tm! tm! tm abs : (tm! tm)! tm gcd : tm! tm! tm let : (tm! tm)! tm! tm fix : (tm! tm)! tm Fig. 3. Constants for Representing Functional Programs type tm correspond to valid programs. Some form of type checking is needed. We only discuss evaluation here and assume terms to be evaluated correspond to valid programs. The rewrite rules expressing evaluation are given in Figure 4. Evaluation in the rst-order fragment of this language is given by all but the last M < M ! false M < (s M )! true (s M ) < M ! false (s M ) < (s N )!M < N (s M ) (s N )!M N 0 M ! 0 M 0!M (N < M ! true) ) (gcd M N ! gcd (M N ) N ) (M < N ! true) ) (gcd M N ! gcd M (N M )) gcd M M !M if true M N !M if false M N ! N hd (cons M N )!M tl (cons M N )! N empty nil ! true empty (cons M N )! false abs x:(app M x)!M 8x:((x! N ) (Mx ! P )) ) (app (abs M ) N ! P ) 8x:((x! N ) (Mx ! P )) ) (let M N ! P ) 8x:((x! (fix M )) (Mx ! P )) ) (fix M ! P ) Fig. 4. Rewrite Rules Expressing Evaluation in a Simple Functional Language four rules. These rules are straightforward and it is easy to see that they satisfy the necessary constraints. The left and right hand sides are patterns since none 6 of the free variables are applied to any arguments, and the two conditions are G-conditions. Now consider the last four rules. The two constants app and abs are used to code function application and abstraction. The rst rule speci es -reduction of -terms. On the left hand side, the bound variable x will not occur in instances of M as is required by the -rule: any instance of M containing x would cause the variable x in the above rule to be renamed to avoid variable capture. The second rule speci es -conversion. A term of the form (app (abs M) N) is a -redex whose reduced form is P as long as the condition on the left is satis ed. This condition states that for an arbitrary x, under the addition of the rewrite rule that rewrites x to N , it must be the case that Mx reduces to P . Note that instances of P cannot contain free occurrences of the variable x bound by universal quanti cation for the same reason as stated above for M . Thus all occurrences of x in Mx must be rewritten to N in order for this condition to succeed. The let construct corresponds to a let statement in ML. In a term of the form (let M N), it is intended that the bound variable at the head ofM will be assigned the value N in the body. In other words, this term is an abbreviation for the applicationMN . This reduction is expressed by the third rule above and is similar to the rule for -reduction. The rule which expresses the unfolding of a xpoint operator is also similar, but here x rewrites to (fix M). It is easy to see that the left and right hand sides of the above four rules are patterns, since there are no arguments to any of the free variables. In the conditions, the free variable M is applied to x which is bound by a positive occurrence of a universal quanti er. In the latter three rules, although P occurs on the right but not on the left of the rewrite rule, it occurs in reduced-term postition in the condition. Thus these conditions are G-conditions, and all four rules are valid conditional rewrite rules. Note that as a rewrite system, these rules express non-deterministic evaluation. Nothing about order of evaluation in speci ed. In Felty [5], we show how to implement various rewriting strategies in the Prolog logic programming language. Such strategies, when given these rewrite rules as a parameter, correspond to various strategies for evaluating functional programs. 4 An Interpreter for L In the next section, we will talk about specifying higher-order conditional rewrite systems in L . We will discuss the operational reading of these speci cations with respect to a logic programming interpreter for L and provide some insight into implementation. We provide a high-level description of this interpreter here. A de nite clause is a D-formula of L , and a program is a set of de nite clauses. A goal is a G-formula. From properties about L presented in Miller [10], a sound and complete (with respect to intuitionistic logic) non-deterministic interpreter can be described by the following search operations. Here, the inter7 preter is attempting to determine if the goal formulaG follows from the program . AND: If G is G1 ^G2 then try to show that both G1 and G2 follow from . AUGMENT: If G is D G0 then add D to the current program and try to show G0. GENERIC: If G is 8 xG0 then pick a new constant c of type and try to show [c=x]G0. BACKCHAIN: If G is atomic, we consider the current program. If there is a universal instance of a de nite clause which is convertible to G then we are done. If there is a de nite clause with a universal instance of the form G0 G then try to show G0 follows from . If neither case holds then G does not follow from . An implementation of an interpreter must make many choices which are left unspeci ed in the high-level description above. We discuss a few of the choices made by the logic programming language Prolog, which contains an implementation of L . First, the order in which conjuncts and disjuncts are attempted and the order for backchaining over de nite clauses is determined exactly as in conventional Prolog systems: conjuncts and disjuncts are attempted in the order they are presented. De nite clauses are backchained over in the order they are listed in using a depthrst search paradigm to handle failures. Logic variables as in Prolog are used in forming a universal instance in the BACKCHAIN operation. These variables can later be instantiated through uni cation. In this case, it is 0-uni cation that is required. The presence of logical variables in an implementation also requires that GENERIC be implemented slightly di erently than is described above. In particular, if the goal 8 xG0 contains logical variables, the new constant c must not appear in the terms eventually instantiated for the logical variables which appear in G0 or in the current program. Any implementation must take this constraint into account. 5 Specifying Rewrite Systems In this section, we discuss the speci cation of higher-order rewrite systems in L . Unlike E , L does not have the equality, reducibility, join, and redex relations or the inference rules for them as primitives. Here, we introduce constants for these relations and provide program clauses to specify the inference rules. We will assume that all terms from a given object language contain only constants from a xed signature, say , which at least includes all of the constants in the rewrite rules. As an example, we take as a signature the set of constants declared in Figure 3 and illustrate the speci cation of the rewrite system for evaluation given in Figure 4. 8 To specify rewriting at a particular primitive type , we introduce the in x relations = , ! , # , and ! to serve as predicates of type ! ! o. Our speci cation will be a set of de nite clauses from which we can attempt to prove goals representing rewriting queries. The speci cation of rewrite rules as clauses is straightforward: we replace the rewriting relations of E with the new rewriting predicates, we replace ) with , and we take the universal closure over the free variables of the rewrite rule, including those in the condition. To illustrate, the three clauses below specify a rst-order rule without and with a condition, and a higher-order rule with a condition. All subscripts on the reducibility and redex relation should be tm. We omit them for readability. 8M8N ((s M) < (s N)!M < N) 8M8N((N < M ! true) (gcd M N ! gcd (M N) N)) 8N8M8P (8x:((x! N) (Mx ! P )) (app (abs M) N ! P )) Note that we do in fact obtainD-formulas by simply taking the universal closure at the top level. Any free variable occurrence in the rewrite rule becomes a positive variable occurrence in the closure. In the condition, both negative and free variable occurrences become positive variable occurrences in the closure. As a formula in G 0, it was the negative and free variables that had to satisfy the head restriction, while in a D-formula, it is the positive occurrences that must satisfy this restriction. Generally, in executing rewrite goals, we will often have a closed term on the left of the arrow and a variable on the right to be instantiated with the result of the rewrite. In using the rst clause for example, M and N will be replaced with logic variables which get instantiated by matching the left hand side of the query with the pattern (s M) < (s N). When the second clause is used in backchaining, we will then have to solve the subgoal specifying the condition. Note that if M and N are instantiated by the backchain operation, the terms on both the left and right of this subgoal will be instantiated. Backchaining on the third clause will provide instances of M and N . The subgoal that must be proved is slightly more complex. First, a GENERIC search operation will be applied to generate a new constant, say c, for x. Then the AUGMENT operation will add a clause stating that this constant c rewrites to the given instance of N . Then, in this new context, it must be shown that Mc rewrites to some term instantiating P . By the restriction on the GENERIC operation, this term cannot contain c, thus the rewrite rule for c must be applied for every instance of c, replacing each one by N . For readability, in the remainder of this and the next sections, we will often leave o outermost universal quanti cation, and assume universal closure over all variables written as tokens with initial upper case letters. To specify congruence, we introduce a D-formula for each constant in . These D-formulas have the same form as those for rewrite rules with conditions. For example, the following two formulas are included for the app and abs 9 constants.(M ! P ) ^ (N ! Q) (app M N ! app P Q)8x((x! x) (Mx ! Nx)) (abs M ! abs N)The clause for abs states that an abstraction (abs M) rewrites to (abs N) if forarbitrary x such that x rewrites to itself, Mx reduces to Nx. Operationally,in trying to solve a goal of the form (abs M 0 ! abs N 0) where, say, M 0 is aclosed term and N 0 a logic variable, we can use this clause to descend throughthe abstraction in M 0. The GENERIC operation will generate a new meta-levelsignature item, say c, and the AUGMENT operation will add the atomic formula(c ! c). This can be viewed as the dynamic addition of a new constant tothe object-level signature and a re exive rule for it. Then, -reduction at themeta-level of M0c performs the substitution of the new item c for the outermostbound variable in M 0. In e ect, the new signature item plays the role of thename of the object level bound variable. The atomic clause (c! c) can be usedduring the search for a term N 0c that is reachable by some number of rewritesteps from M 0c. N 0 is the abstraction not containing c.A congruence rule for a new constant of functional type is more complex. Forexample, if we had a function constant h whose type is ((tm! tm)! tm)! tm,its corresponding congruence clause would be:8f(8P8Q((P ! Q) (fP ! fQ)) (Mf ! Nf)) (h M ! h N)Operationally, after backchaining on the above clause, instead of an atomicclause, the clause (P ! Q) (fP ! fQ) would be dynamically added byAUGMENT, serving as new congruence rule for the new function constant f .We can in fact de ne a general function for specifying congruence rules fora particular signature. For signature item a of type , [[a; a : ]] yields thenecessary congruence rule.[[M ;N : ]] = (M ! Nif is a primitive type8x8y([[x; y :1]]+ [[Mx;Ny : 2]] ) if is 1 ! 2[[M ;N : ]]+ = (M ! Nif is a primitive type8x([[x; x : 1]] [[Mx;Nx :2]]+) if is 1 ! 2These functions are a (corrected) version of those used by Miller [11] to specifyequality and substitution for simply typed -terms and are similar to those usedby Felty [4] to code a dependent typed -calculus in a higher-order intuitionisticlogic.The remaining rules of Figure 2 are speci ed in a straightforward manner,by including the following clauses at each primitive type.(M ! P ) ^ (P ! N) (M ! N)(M # P ) ^ (N # P ) (M ! N)(M ! N) (M = N)(M ! N) (N = M)10 Note that we do not include a clause specifying re exivity. We do not needone when we include \re exivity" clauses (a! a) for each constant a of primitivetype in the signature. To rewrite a term to itself, congruence rules must beused to descend through the entire term, applying re exivity rules for constantsat the leaves. For e ciency reasons, we may want to include a re exive rule8M(M !M). It can be used to prove the equivalence of two arbitrary terms ofprimitive type in a single step. An advantage of the former approach is that itcan also be used to verify that a term is well-formed in a particular signature.6 Some Concluding RemarksAs stated earlier, the formulation of rewrite rules in Nipkow [12] and Felty [5] isslightly di erent than that given here. There are no conditions and right handsides are not restricted to be patterns. If terms are always matched against lefthand sides, decidability of the rewrite relation is not a ected. In this setting,the last three rules in Figure 4 can be expressed more directly, and perhapsmore naturally, as follows.app (abs M) N ! MNfix M ! M (fix M)let M N ! MNIn applying one of these rules, instead of having to satisfy a condition which addsa rewrite rule for some new constant c, and must then reduce the term Mc in anew context, application of -terms is used to directly substitute the appropriateterm for the bound variable inM . In that setting, a logic programming languagewith uni cation more powerful than 0 -uni cation is needed, such as Prolog.Any rewrite system expressible in the more general setting can in fact beexpressed as conditional rewrite rules as de ned here. A function can be de nedto translate rules with arbitrary -terms on the right to conditional rules withpatterns on the right. Such a function is de ned by induction over types ina manner similar to the one in the previous section for generating congruenceclauses.Within theorem proving systems, capabilities for higher-order rewriting canprovide a useful tool for the manipulation of formulas and programs. In [5], weillustrate how to integrate a general component for higher-order term rewritinginto a tactic style theorem prover. The implementation discussed there builds onthe logic programming implementation of tactic style theorem provers presentedin Felty [6], and provides a setting for implementing both general and speci crewriting strategies. These techniques can be easily extended to conditionalrewriting.References1. Peter Aczel. A general church-rosser theorem. Technical report, University ofManchester, 1978.11 2. J. A. Bergstra and J. W. Klop. Conditional rewrite rules: Con uence and termi-nation. Journal of Computer and System Sciences, 32:323{362, 1986.3. N. Dershowitz, M. Okada, and G. Sivakumar. Con uence of conditional rewritesystems. In S. Kaplan and J.-P. Jouannaud, editors, Proceedings of the First Inter-national Workshop on Conditional Term Rewriting Systems, pages 31{44. Springer-Verlag Lecture Notes in Computer Science, 1987.4. Amy Felty. Encoding dependent types in an intuitionistic logic. In G erard Huetand Gordon Plotkin, editors, Logical Frameworks, pages 215{251. Cambridge Uni-versity Press, 1991.5. Amy Felty. A logic programming approach to implementing higher-order termrewriting. In Lars-Henrik Eriksson, Lars Hallnas, and Peter Schroeder-Heister,editors, Proceedings of the January 1991 Workshop on Extensions to Logic Pro-gramming, pages 135{161. Springer-Verlag Lecture Notes in Arti cial Intelligence,1992.6. Amy Felty. Implementing tactics and tacticals in a higher-order logic programminglanguage. Journal of Automated Reasoning, To appear.7. J. Roger Hindley and Jonathan P. Seldin. Introduction to Combinatory Logic andLambda Calculus. Cambridge University Press, 1986.8. St ephane Kaplan. Conditional rewrite rules. Theoretical Computer Science,33:175{193, 1984.9. J. W. Klop. Combinatory reduction systems. Technical Report MathematicalCentre Tracts Nr.127, Centre for Mathematics and Computer Science, Amsterdam,1980.10. Dale Miller. A logic programming language with lambda-abstraction, functionvariables, and simple uni cation. Journal of Logic and Computation, 1(4):497{536, 1991.11. Dale Miller. Uni cation of simply typed lambda-terms as logic programming. InEighth International Logic Programming Conference, Paris, France, June 1991.MIT Press.12. Tobias Nipkow. Higher-order critical pairs. In Sixth Annual Symposium on Logicin Computer Science, pages 342{349, Amsterdam, July 1991.12
منابع مشابه
A Reduction Ordering for Higher - Order
We investigate one of the classical problems of the theory of term rewriting, namely termination. We present an ordering for comparing higher-order terms that can be utilized for testing termination and decreasingness of higher-order conditional term rewriting systems. The ordering relies on a rst-order interpretation of higher-order terms and a suitable extension of the RPO. 1 Motivation Term ...
متن کاملA Logic for Rewriting Strategies
As term-rewriting has matured, with applications in areas such as automatic program transformation, the necessity of programmed control for applying rules has become apparent. Early term-rewriting systems used a xed strategy, such as innermost-leftmost matching, choosing among possibly overlapping rules nondeterministically. This mode of control is su cient for a con uent, terminating system of...
متن کاملA Call-by-Need Strategy for Higher-Order Functional-Logic Programming
We present an approach to truely higher order functional logic programming based on higher order narrowing Roughly speaking we model a higher order functional core language by higher order rewriting and extend it by logic variables For the integration of logic programs conditional rules are supported For solving goals in this framework we present a complete calcu lus for higher order conditiona...
متن کاملA Logic Programming Approach to Implementing Higher-Order Term Rewriting
Term rewriting has proven to be an important technique in theorem proving. In this paper, we illustrate that rewrite systems and strategies for higher-order term rewriting, which includes the usual notion of rst-order rewriting, can be naturally speciied and implemented in a higher-order logic programming language. We adopt a notion of higher-order rewrite system which uses the simply typed-cal...
متن کاملCompleteness of Narrowing for Orthogonal Conditional Rewrite Systems
Conditional narrowing is underlying computational mechanism of most functional-logic programming languages. It is important to ensure completeness of conditional narrowing for conditional term rewriting systems without assumption of termination because termination is a too severe restriction for programming languages. In this paper we show completeness of conditional narrowing for a kind of ort...
متن کاملA Complete Narrowing Calculus for Higher-Order Functional Logic Programming
Using higher-order functions is standard practice in functional programming, but most functional logic programming languages that have been described in the literature lack this feature. The natural way to deal with higher-order functions in the framework of ( rst-order) term rewriting is through so-called applicative term rewriting systems. In this paper we argue that existing calculi for lazy...
متن کامل